package model
{
	import model.vo.AbstractPageItem;
	import model.vo.AbstractPageVO;
	import model.vo.UserVO;
	
	import mx.collections.ArrayCollection;
	
	[Bindable]
	public class ModelLocator
	{
		static private var _instance:ModelLocator;
		
		/**
		 * Data Members
		 * */
		public var pages:ArrayCollection = new ArrayCollection();
		public var config:Object = {};
		
		public var user:UserVO = new UserVO();;
		
		static public function get Instance():ModelLocator
		{
			if(!_instance) _instance = new ModelLocator();
			return _instance;
		}
		
		public function ModelLocator()
		{
			if(_instance) throw new Error("Singleton, use .Instance instead");
		}
		
		public function storeConfig(data:Object):void
		{
			var xml:XML = new XML(data);
			var pageList:XMLList = xml..children().children();
			
			var l:int = pageList.length();
			var i:int = 0;
			for(; i < l; ++i)
			{
				var vo:AbstractPageVO = new AbstractPageVO();
				vo.url = (pageList[i] as XML).children();
				pages.addItem(vo);
			}
		}
		
		public function storePage(data:Object, count:int):void
		{
			var xml:XML = new XML(data);
			var items:XMLList = xml.children();
			var vo:AbstractPageVO = AbstractPageVO(pages.getItemAt(count-1));
			vo.title = xml..title;
			
			items = items[1].children();
			
			var l:int = items.length();
			var i:int = 0;
			var item:AbstractPageItem;
			for(; i < l; ++i)
			{
				item = new AbstractPageItem();
				item.url = items[i]..url;
				item.copy = items[i]..copy;
				vo.items.push(item);
			}
		}

	}
}